package lt.liepasoft.sudoku;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SudokuView extends JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton generate, showSolution;
//    private JComboBox difficulties;
    private GridBagConstraints c;
    private PuzzlePanel puzzlePanel;

    private SudokuTable puzzle, solved;

    private SudokuPuzzleMaker puzzleFactory;
    
    public interface Data {

        String[] difficulties = {
            "Easy",
            "Medium",
            "Hard"
        };

        int[][] remove = {
            {0,30}, 
            {30,50},
            {50,81}
        };
    }    
    
    public SudokuView(){
        super(new GridBagLayout());
        c = new GridBagConstraints();

        generate = new JButton("Generate");
        generate.addActionListener(new GenerateAction());

        showSolution = new JButton("Show Solution");
        showSolution.addActionListener(new ShowSolutionAction());

//        difficulties = new JComboBox(Data.difficulties);

        puzzlePanel = new PuzzlePanel();

        c.gridx=c.gridy = 0;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = 2;

//        add(difficulties, c);
        c.gridy++;
        add(puzzlePanel, c);
        c.gridy++;
        c.gridwidth = 1;
        add(generate, c);
        c.gridx = 1;
        add(showSolution, c);


        //table = new SudokuTable(9,9);
        puzzleFactory = new SudokuPuzzleMaker();


        JFrame f = new JFrame("Sudoku puzzle generator");
        f.setSize(400, 500);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setContentPane(this);

    }

    class PuzzlePanel extends JPanel {

        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private int
                offsetX = 20,
                offsetY = 32,
                space = 30;

        private SudokuTable puzzle;


        public PuzzlePanel(){
            this.setPreferredSize(new Dimension(300,300));
            this.setOpaque(true);
            this.setBorder(BorderFactory.createLineBorder(Color.black));
        }

        public void setPuzzle(SudokuTable puzzle){
            this.puzzle = puzzle;
            updateUI();
        }

        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.white);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            if (puzzle == null) return;

            int x = offsetX,
                y = offsetY;

            g.setColor(Color.black);
            for (int rowIndex=0; rowIndex<puzzle.gridSize; rowIndex++){
            	int[] line = puzzle.getRow(rowIndex);
                x = offsetX;
                for (int value: line){
                    if (0 < value && value < 10){
                        g.drawString(value+"", x, y);
                    } else
                        g.drawString(" ", x, y);
                    
                    y -= 20;
                    x -= 5;
                    g.drawLine(x, y, x+space, y);
                    g.drawLine(x, y, x, y+space);
                    g.drawLine(x, y+space, x+space, y+space);
                    g.drawLine(x+space, y, x+space, y+space);
                    y += 20;
                    x += 5;
                    x += space;
                }
                y += space;
            }
        }
    }

    class GenerateAction implements ActionListener {
        public void actionPerformed(ActionEvent e){
        	solved = puzzleFactory.generateUniqueSudoku();
        	puzzle = puzzleFactory.createPuzzle(solved);
            puzzlePanel.setPuzzle(puzzle);
        }
    }

    class ShowSolutionAction implements ActionListener {
        public void actionPerformed(ActionEvent e){
            puzzlePanel.setPuzzle(solved);
        }
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new SudokuView();
	}
	
	

}
